﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace OpenExpressApp.WPF.Themes
{
    public static class ThemeManager
    {
        //当前加载的主题资源文件
        private static ResourceDictionary currentResources;
        public static ResourceDictionary GetThemeResourceDictionary(string theme)
        {
            if (theme != null)
            {
               // Assembly assembly = Assembly.LoadFrom("OpenExpressApp.Module.WPF.Style");
                string packUri = String.Format(@"/OpenExpressApp.Module.WPF.Style;component/{0}/Theme.xaml", theme);
                return Application.LoadComponent(new Uri(packUri, UriKind.Relative)) as ResourceDictionary;
            }
            return null;
        }

        public static string[] GetThemes()
        {
            string[] themes = new string[] 
            { 
                "White","Blue",
                //"ExpressionDark", "ExpressionLight", "RainierOrange", "RainierPurple", "RainierRadialBlue", 
                //"ShinyBlue", "ShinyRed", "ShinyDarkTeal",  "ShinyDarkGreen", "ShinyDarkPurple",
                //"DavesGlossyControls", "WhistlerBlue", "BureauBlack", "BureauBlue", "BubbleCreme", 
                //"UXMusingsRed", "UXMusingsGreen", "UXMusingsRoughRed", "UXMusingsRoughGreen", "UXMusingsBubblyBlue"
            };
            return themes;
        }

        public static void ApplyTheme(this Application app, string theme)
        {
            ResourceDictionary dictionary = ThemeManager.GetThemeResourceDictionary(theme);

            if (dictionary != null)
            {
                app.Resources.MergedDictionaries.Remove(currentResources);
                currentResources = dictionary;
                app.Resources.MergedDictionaries.Add(dictionary);
            }
        }

        public static void ApplyTheme(this ContentControl control, string theme)
        {
            ResourceDictionary dictionary = ThemeManager.GetThemeResourceDictionary(theme);

            if (dictionary != null)
            {
                control.Resources.MergedDictionaries.Remove(currentResources);
                currentResources = dictionary;
                control.Resources.MergedDictionaries.Add(dictionary);
            }
        }

        #region Theme

        /// <summary>
        /// Theme Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty ThemeProperty =
            DependencyProperty.RegisterAttached("Theme", typeof(string), typeof(ThemeManager),
                new FrameworkPropertyMetadata((string)string.Empty,
                    new PropertyChangedCallback(OnThemeChanged)));

        /// <summary>
        /// Gets the Theme property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static string GetTheme(DependencyObject d)
        {
            return (string)d.GetValue(ThemeProperty);
        }

        /// <summary>
        /// Sets the Theme property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static void SetTheme(DependencyObject d, string value)
        {
            d.SetValue(ThemeProperty, value);
        }

        /// <summary>
        /// Handles changes to the Theme property.
        /// </summary>
        private static void OnThemeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            string theme = e.NewValue as string;
            if (theme == string.Empty)
                return;
            //该为整个应用程序主题一致
            Application.Current.ApplyTheme(theme);
            //ContentControl control = d as ContentControl;
            //if (control != null)
            //{
            //    control.ApplyTheme(theme);
            //}
        }

        #endregion



    }
}
